import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/user/Users.vue'

import Goods from '../views/goods/Goods.vue'
import addGoods from '../views/goods/add.vue'
import Categories from '../views/goods/Categories.vue'
import Params from '../views/goods/Params.vue'
import Par from '../views/goods/Par.vue'


import Roles from '../views/roles/Roles.vue'
import Rights from '../views/roles/Rights.vue'








Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/Login' }, //如果是/重定向到 login页面
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/goods', name: 'goods', component: Goods },
      { path: '/categories', name: 'categories', component: Categories },
      { path: '/params', name: 'params', component: Params },
      { path: '/par', name: 'par', component: Par },
      { path: '/roles', name: 'roles', component: Roles },
      { path: '/rights', name: 'rights', component: Rights },
      { path: '/addGoods', name: 'addGoods', component: addGoods },

    ]
  },


  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: "history",
  base: __dirname,
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
